package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.util.RecyclerViewStateUtils;
import com.github.jdsjlzx.view.LoadingFooter;
import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.adapter.MineOrderListAdapter;
import com.hdcx.Minfresh.bean.OrderListWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class MineOrderListActivity extends BaseActivity {
    LRecyclerView recyclerview;
    MineOrderListAdapter adapter;
    int line_tag = 0;//选中哪一个，默认第一个
    int type = 0; //0全部，1代付款，10带配送，20配送中，30已到达
     View[] lines;
    Boolean isRefresh = false;
    int num = 1;
    int totleNum;
    OrderListWrapper data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_order_list);
        initView();
        initData();
    }

    private void initView() {
        recyclerview = (LRecyclerView) findViewById(R.id.recycler_view);
        adapter = new MineOrderListAdapter(this);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(new LRecyclerViewAdapter(adapter));
        setrecycleView();
        ((TextView) findViewById(R.id.tv_title)).setText("全部订单");
        findViewById(R.id.tv_left).setOnClickListener(this);
        type = getIntent().getIntExtra("type", 0);
        View[] layouts = new View[]{findViewById(R.id.layout_1), findViewById(R.id.layout_2), findViewById(R.id.layout_3), findViewById(R.id.layout_4), findViewById(R.id.layout_5)};
        lines = new View[]{findViewById(R.id.tv_line1), findViewById(R.id.tv_line2), findViewById(R.id.tv_line3), findViewById(R.id.tv_line4), findViewById(R.id.tv_line5)};
        setLine(type);
        for (int i = 0; i < layouts.length; i++) {
            final int finalI = i;
            layouts[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lines[line_tag].setVisibility(View.GONE);
                    lines[finalI].setVisibility(View.VISIBLE);
                    line_tag = finalI;
                    switch (finalI) {
                        case 0:
                            type = 0;
                            break;
                        case 1:
                            type = 1;
                            break;
                        case 2:
                            type = 10;
                            break;
                        case 3:
                            type = 20;
                            break;
                        case 4:
                            type = 30;
                            break;
                    }
                    num =1;
                    post_main();
                }

            });
        }

    }

    private void setrecycleView() {
        recyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                num = 1;
                isRefresh = true;
                post_main();
            }
        });
        recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (num * 10 < totleNum) {
                    // loading more
                    num = num + 1;
                    RecyclerViewStateUtils.setFooterViewState(MineOrderListActivity.this, recyclerview, 10, LoadingFooter.State.Loading, null);
                    post_main();
                } else {
                    //the end
                    RecyclerViewStateUtils.setFooterViewState( recyclerview, LoadingFooter.State.TheEnd);
                }
            }
        });
    }

    private void setLine(int type) {
        int line = 0;
        switch (type) {
            case 0:
                line = 0;
                break;
            case 1:
                line = 1;
                break;
            case 10:
                line = 2;
                break;
            case 20:
                line = 3;
                break;
            case 30:
                line = 4;
                break;
        }
        lines[line_tag].setVisibility(View.GONE);
        lines[line].setVisibility(View.VISIBLE);
        line_tag = line;
    }

    private void initData() {
        post_main();
    }

   /* startdate	//起始日期 可选
    enddate		//结束日期 可选
    type  //状态 可选 默认全部
    page	页码	可选
    size    每页数量  可选*/

    private void post_main() {
        UserInfoBean user = ShpfUtil.getObject(ShpfUtil.LOGIN);
        AppUtil.showLoading(this);
        JSONObject json = new JSONObject();
        try {
            json.put("uid", user.getUserinfo().getId());
            json.put("type", type);
            json.put("page", num);
            json.put("size", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.ORDER)
                .content(json.toString())
                .build()
                .execute(new Callback<OrderListWrapper>() {
                    @Override
                    public OrderListWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, OrderListWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        AppUtil.closeLoading();
                    }

                    @Override
                    public void onResponse(OrderListWrapper response) {
                        AppUtil.closeLoading();
                        if (response.getStatus() == 1) {
                            adapter.setData(response);
                            totleNum = response.getCount();
                            if (num == 1) {
                                data = response;
                                adapter.setData(response);
                            } else {
                                data.getOrderlist().addAll(response.getOrderlist());
                                adapter.setData(data);
                            }
                        }
                        if (isRefresh) {
                            recyclerview.refreshComplete();
                        }
                    }
                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_left:
                finish();
                break;
        }
    }
}
